package com.bysj.service;

import com.bysj.dao.NewsDao;
import com.bysj.entity.Goods;
import com.bysj.entity.News;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    NewsDao newsDao;
    @Override
    public List<News> getAll(Map map) {
        return newsDao.getAll(map) ;
    }

    @Override
    public List<News> selectById(Integer id) {
        return newsDao.selectById(id);
    }

    //根据id删除资讯
    @Override
    public void deleteNewsById(Integer id) {
        newsDao.deleteNewsById(id);
    }
    //添加资讯
    @Override
    public void addNews(News news) {
        SimpleDateFormat fmt= new SimpleDateFormat("yyyy-MM-dd");
        Date currentTime = new Date();
        String dateString = fmt.format(currentTime);
        news.setDate(dateString);
        newsDao.addNews(news);
    }

    @Override
    public PageInfo<News> getAllNewsByPage(Map map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> nesList = newsDao.getAll(map);
        PageInfo<News> pageInfo = new PageInfo<News>(nesList);
        return pageInfo;
    }
}
